package com.example.springboot.controller;

import java.util.List;

import javax.annotation.Resource;

import com.example.springboot.bean.PageResult;
import com.example.springboot.bean.Result;
import com.example.springboot.bean.User;
import com.example.springboot.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	//插入
	@RequestMapping("/insertUser")
	public Result insertUser(@RequestBody User user) {
		try {
			userService.insertUser(user);
			return new Result(true,"添加成功");
		}catch (Exception e){
			e.printStackTrace();
			return new Result(false,"添加失败");
		}

	}
	//没有用到
	/**
	@RequestMapping("/insertGetKey")
	public User insertGeyKey(User user) {
		userService.insertGetKey(user);
		return user;
	}**/

	//基于名字查询
	@RequestMapping("/selectByUsername")
	public User selectByUsername(String username) {

		return userService.selectByUsername(username);
	}
	//查找全部
	@RequestMapping("/findAll")
	public List<User> findAll(){
		return userService.findAll();
	}
	//修改
	@RequestMapping("/update")
	public Result update(@RequestBody User user) {
		try {
			userService.update(user);
			System.out.println("success");
			return new Result(true,"update success");

		}catch (Exception e){
			e.printStackTrace();
			return new Result(false,"update fail");
		}
	}
	//删除
	@RequestMapping("/delete")
	public Result delete(long ids[]) {
		try {
			userService.dele(ids);
			return new Result(true,"delete success");

		}catch (Exception e){
			e.printStackTrace();
			return new Result(false,"delete fail");
		}
	}
	//分页功能实现
	@RequestMapping("/findPage")
	public PageResult findpage(Integer page,Integer size){
		return userService.findPage(page,size);
	}
	//基于id查询
	@RequestMapping("/findOne")
	public User findOne(Integer id){
		return userService.findUserById(id);
	}
}
